package com.kwokstudio.fuselage;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.kwokstudio.fuselage.bean.ToolbarEvent;
import com.kwokstudio.fuselage.ui.first.IndexFragment;
import com.kwokstudio.fuselage.ui.zero.ZeroFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private int currentFragment = -1;
    private List<Fragment> fragments = new ArrayList<>();

    //一些控件
    private DrawerLayout drawer;
    private Toolbar toolbar;
    private Fragment fragment;
    private ZeroFragment zeroFragment;
    private IndexFragment indexFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EventBus.getDefault().register(this);//注册一个EventBus
        setDrawer();
        initFragments();
        switchFragment(0);

    }


    @Subscribe
    public void onEventMainThread(ToolbarEvent event) {
        switch (currentFragment) {
            case 0://知乎日报的Fragment
                toolbar = (Toolbar) zeroFragment.getView().findViewById(R.id.toolbar);
                ActionBarDrawerToggle toggle0 = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
                toggle0.syncState();
                setTitle("知乎日报");
                break;
            case 2://
                toolbar = (Toolbar) indexFragment.getView().findViewById(R.id.index_toolbar);
                ActionBarDrawerToggle toggle1 = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
                toggle1.syncState();
                break;
        }
    }


    /*
         *设置抽屉
         *
         */
    private void setDrawer() {
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }


    /*
     *切换Fragment，参数为int index
     *
     */
    private void switchFragment(int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if ((fragments.size() - 1) < index) { //说明没有这个fragment
            switch (index) {
                case 0:
                    fragment=new ZeroFragment();
                    break;
                case 1:
                    fragment=new IndexFragment();
                    break;
            }
            transaction.add(R.id.fl_container,fragment);
            fragments.add(fragment);
        }else {
            Fragment toFragment = fragments.get(index); //  获得要添加的碎片

            if (toFragment.isAdded()) {  // 判断是否已经生成过
                transaction.show(toFragment);    //有的话直接显示
            } else {
                transaction.add(R.id.fl_container, toFragment);   // 如果没有 直接创建
            }
        }

        if (currentFragment!=-1){   //隐藏之前的
            transaction.hide(fragments.get(currentFragment));
        }
        transaction.commit();       //提交

        //设置Toolbar用
        currentFragment = index;
    }

    /*
     *初始化Fragment集合
     *
     */
    private List<Fragment> initFragments() {
        fragments.add(new ZeroFragment());
        fragments.add(new IndexFragment());
        return null;
    }


    /*
     *抽屉打开时，返回事件为关闭
     *
     */
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            Snackbar.make(findViewById(R.id.fl_container), "退出？", Snackbar.LENGTH_SHORT).setAction("嗯", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                    overridePendingTransition(0, 0);
                }
            }).show();
        }
    }

    /*
     *抽屉导航栏菜单
     *
     */
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        switch (id) {
            case R.id.nav_read:
                switchFragment(0);
                break;
            case R.id.nav_index:
                switchFragment(1);
                break;

        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
